package com.mjfeed.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description 商品类别
 * @author zhaominglei
 * @date 2014年9月21日
 */
@Entity
@Table(name="feed_product_category")
public class ProductCategory extends OrderEntity implements Serializable {
	private static final long serialVersionUID = -7903073373331703548L;
	/** 树路径分隔符 */
	public static final String TREE_PATH_SEPARATOR = ",";
	/** 层级 */
	private Integer grade;
	/** 名称 */
    private String name;
    /** 图片 **/
	private String source;
    /** 页面描述 */
    private String seoDescription;
    /** 页面关键词 */
    private String seoKeywords;
    /** 页面标题 */
    private String seoTitle;
    /** 树路径 */
    private String treePath;
    /** 上级分类 */
	private ProductCategory parent;
	/** 下级分类 */
	private Set<ProductCategory> children = new HashSet<ProductCategory>();
	/** 商品 */
    private Set<Product> products = new HashSet<Product>();
    /** 论坛话题 */
    private Set<ForumTopic> forumTopics = new HashSet<ForumTopic>();
    /** 论坛回复*/
    private Set<ForumReply> forumReplies = new HashSet<ForumReply>();
    
    /** 文件 */
	private MultipartFile file;

	public Integer getGrade() {
		return grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}
	
	@Column(name="seo_description")
	public String getSeoDescription() {
		return seoDescription;
	}

	public void setSeoDescription(String seoDescription) {
		this.seoDescription = seoDescription;
	}

	@Column(name="seo_keywords")
	public String getSeoKeywords() {
		return seoKeywords;
	}

	public void setSeoKeywords(String seoKeywords) {
		this.seoKeywords = seoKeywords;
	}

	@Column(name="seo_title")
	public String getSeoTitle() {
		return seoTitle;
	}

	public void setSeoTitle(String seoTitle) {
		this.seoTitle = seoTitle;
	}

	@Column(name="tree_path")
	public String getTreePath() {
		return treePath;
	}

	public void setTreePath(String treePath) {
		this.treePath = treePath;
	}

	@OneToMany(mappedBy="productCategory", fetch=FetchType.LAZY)
	public Set<Product> getProducts() {
		return products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}

	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="parent_id")
	public ProductCategory getParent() {
		return parent;
	}

	public void setParent(ProductCategory parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy="parent", fetch=FetchType.LAZY)
	@OrderBy("orders asc")
	public Set<ProductCategory> getChildren() {
		return children;
	}

	public void setChildren(Set<ProductCategory> children) {
		this.children = children;
	}
	
	@OneToMany(mappedBy="productCategory", fetch=FetchType.LAZY)
	@OrderBy("id desc")
	public Set<ForumTopic> getForumTopics() {
		return forumTopics;
	}

	public void setForumTopics(Set<ForumTopic> forumTopics) {
		this.forumTopics = forumTopics;
	}
	
	@OneToMany(mappedBy="productCategory", fetch=FetchType.LAZY)
	@OrderBy("id desc")
	public Set<ForumReply> getForumReplies() {
		return forumReplies;
	}

	public void setForumReplies(Set<ForumReply> forumReplies) {
		this.forumReplies = forumReplies;
	}
	
	/**
	 * 获取树路径
	 * 
	 * @return 树路径
	 */
	@Transient
	public List<Long> getTreePaths() {
		List<Long> treePaths = new ArrayList<Long>();
		String[] ids = StringUtils.split(getTreePath(), TREE_PATH_SEPARATOR);
		if (ids != null) {
			for (String id : ids) {
				treePaths.add(Long.valueOf(id));
			}
		}
		return treePaths;
	}
	
	@Transient
	public MultipartFile getFile() {
		return file;
	}
	public void setFile(MultipartFile file) {
		this.file = file;
	}
	/**
	 * 判断是否为空
	 * 
	 * @return 是否为空
	 */
	@Transient
	public boolean isEmpty() {
		return (getFile() == null || getFile().isEmpty()) && StringUtils.isEmpty(getSource());
	}
}
